package edu.fudan.jwxt.service.Impl;

import cn.hutool.core.lang.Pair;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.fudan.jwxt.Exception.ServiceException;
import edu.fudan.jwxt.common.Constants;
import edu.fudan.jwxt.dao.CourseTimeDao;
import edu.fudan.jwxt.entity.Course;
import edu.fudan.jwxt.entity.CourseTime;
import edu.fudan.jwxt.service.CourseTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static edu.fudan.jwxt.utils.TimeUtils.numberWeekDays;

@Service
public class CourseTimeServiceImpl extends ServiceImpl<CourseTimeDao, CourseTime> implements CourseTimeService {
    @Autowired
    private CourseTimeDao courseTimeDao;



    // 不作检查
    @Override
    public List<CourseTime> selectByFirstString(String firstString, Object ObjectNumber){
        Map<String,Object> map = new HashMap<>();
        map.put(firstString,ObjectNumber);
        List<CourseTime> templist = courseTimeDao.selectByMap(map);
        return templist;
    }

    @Override
    public List<CourseTime> selectByClassId(Integer classId){
        List<CourseTime> templist=selectByFirstString("class_id",classId);
        if(templist.isEmpty())
            throw new ServiceException(Constants.CODE_600,"查询课程时间时出现不存在的课程Id");
        return templist;
    }
    
    // 这个更多有更新的意味
    @Override
    public void updateTimeByClassId(Integer classId, List<Pair<Integer, Integer>> list) {
        // 删除所有含classId的项
        Map<String,Object> map = new HashMap<>();
        map.put("class_id",classId);
        courseTimeDao.deleteByMap(map);

        for(Pair<Integer, Integer> p : list){
            CourseTime courseTime=new CourseTime(classId,p.getKey(),p.getValue());
            this.save(courseTime);
        }
    }

    // 插入新课程时间
    @Override
    public void saveTimeByClassId(Integer classId, List<Pair<Integer, Integer>> list){
        List<CourseTime> templist=selectByFirstString("class_id",classId);
        if(!templist.isEmpty())
            throw new ServiceException(Constants.CODE_600,"插入新课程时间时已存在课程Id");
        updateTimeByClassId(classId,list);
    }

    // 删除老课程时间
    @Override
    public void deleteTimeByClassId(Integer classId){
        List<CourseTime> templist=selectByFirstString("class_id",classId);
        if(templist.isEmpty())
            throw new ServiceException(Constants.CODE_600,"删除课程时间不存在");
        List<Pair<Integer, Integer>> list=new ArrayList<>();
        updateTimeByClassId(classId,list);
    }

    // 转换List<CourseTime>为exactClassTime
    @Override
    public List<String> Transfer(List<CourseTime> courseTimeList){
        String tempStr=new String();
        List<String> ans=new ArrayList<>();
        for (CourseTime ct:courseTimeList
             ) {

            tempStr=Integer.toString(ct.getWeekDay())+"-"+Integer.toString(ct.getTimeId());
            ans.add(tempStr);
        }
        return ans;
    }

    // 从List<CourseTime>到timeString的函数
    @Override
    public String TransferToTs(List<CourseTime> courseTimeList){
        String tempStr=new String();
        Comparator<CourseTime> newComp=new Comparator<CourseTime>() {
            @Override
            public int compare(CourseTime o1, CourseTime o2) {
                return (o1.getWeekDay()-o2.getWeekDay())*20+o1.getTimeId()-o2.getTimeId();
            }
        };
        Collections.sort(courseTimeList,newComp);
        Integer A=0;
        for (CourseTime ct :courseTimeList
             ) {
            Integer Anew= ct.getWeekDay();
            Integer B=ct.getTimeId();
            if(A.equals(Anew))
                tempStr+=","+Integer.toString(B);
            else{
                A=Anew;
                tempStr+=" "+numberWeekDays.get(A)+" "+Integer.toString(B);
            }
        }
        return tempStr;
    }
}
